package com.share.example.selenium.service.hzhc.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.share.common.utils.HttpUtil;
import com.share.example.selenium.model.hzhc.task.TaskDetail;
import com.share.example.selenium.model.hzhc.task.TaskUserInfo;
import com.share.example.selenium.service.AbsDriver;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.WebDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
@Slf4j
@Service
public class HcxxTaskService extends AbsDriver {
    private static final String LOGIN_URL = "http://task.ihanchen.com/task-management/app/login";
    private static final String TASKID_DETAIL_URL = "http://task.ihanchen.com/task-management/app/ttaskworkhours/list?page=1&limit=5";
    private static final String SAVE_TASK_URL = "http://task.ihanchen.com/task-management/app/ttaskworkhours/save?token=";
    @Autowired
    protected WebDriver driver;

    @Override
    public void reInitDriver(WebDriver driver) {
        this.driver = driver;
    }

    //登录任务系统
    public TaskUserInfo login(String username, String password) {
        TaskUserInfo taskUserInfo = null;

        Map<String, Object> loginObj = new HashMap<>();
        loginObj.put("username", username);
        loginObj.put("loginName", username);
        loginObj.put("password", password);
        loginObj.put("passWord", password);
        String resultToken = null;
        try {
            resultToken = HttpUtil.postForm(LOGIN_URL, loginObj, null);
            taskUserInfo = JSON.parseObject(resultToken, TaskUserInfo.class);
        } catch (Exception e) {
            log.error("登录失败用户信息={}", JSON.toJSON(loginObj));
        }
        return taskUserInfo;
    }

    //列出任务的详情
    public List<TaskDetail> listTaskDetail(String taskid, String token) {
        //4.获取任务列表
        String url = TASKID_DETAIL_URL + "&taskid=" + taskid + "&token=" + token;
        try {
            String result = HttpUtil.get(url);
            JSONObject task = JSON.parseObject(result);
            Object taskList = task.get("data");
            if ("0".equals(task.getString("code")) && taskList != null) {
                List<TaskDetail> taskDetails = JSON.parseArray(taskList.toString(), TaskDetail.class);
                return taskDetails;
            }
        } catch (Exception e) {
            log.error("任务详情获取失败");
        }

        return null;
    }

    //写日志
    public boolean addTaskLog(String token, String taskId, String strWriteDate, String content, String reportUserId) {
        boolean writeSuccess = false;
        String saveUrl = SAVE_TASK_URL + token;
        Map<String, Object> param = new HashMap<>();
        param.put("workdate", strWriteDate);/*填报的日志*/
        param.put("taskid", taskId);
        param.put("content", content);
        param.put("userids", reportUserId);
        param.put("normalWh", 8);
        param.put("overtimeWh", 0);
        try {
            String post = HttpUtil.post(saveUrl, JSON.toJSONString(param));
            if ("0".equals(JSON.parseObject(post).getString("code"))) {
                writeSuccess = true;
            }
        } catch (Exception e) {
            log.error("日志填写异常", e);
        }
        return writeSuccess;
    }
}
